package com.example.learnspringboot.mapper;

import com.example.learnspringboot.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Mapper
//将Usermapper注入到spring容器中
public interface UserMapper {

    @Insert("insert into `user` (username, password, name, phone, email, address, avatar) " +
            "values (#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{avatar})")
    void insert(User user);

    @Update("update `user` set username = #{username}, password = #{password}, name = #{name}, phone = #{phone}, email = #{email}, address = #{address}, avatar = #{avatar} where id = #{id}")
    //要求更改的条件里必须传一个id,id是数据唯一的标识
    void update(User user);

    @Delete("delete from `user` where id = #{id}")
    void delete(Integer id);

    @Delete("delete from `user` where id in (1,2,3,4,5)")
    void batchDeleteUser(List<Integer> ids);

    @Select("select * from `user`")
    List<User> searchAll();

    @Select("select * from `user` where id = #{id} order by id desc")
    User selectById(Integer id);

    @Select("select * from `user` where username = #{username} order by id desc")
    User selectByUserName(String username);
}
